/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.constant.enums;

import com.comac.ins.common.core.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.text.DecimalFormat;

/**
 * 文件大小
 *
 * @author hxloongs
 * @date 2025-01-06
 */
@AllArgsConstructor
@Getter
public enum PartFileSizeUnitEnum {
    KB("KB", "KB"),
    MB("MB", "MB"),
    GB("GB", "GB");


    private final String value;
    private final String desc;

    public static String getDescByValue(String value) {
        PartFileSizeUnitEnum partFileSizeUnitEnum = forValue(value);
        if (partFileSizeUnitEnum == null) {
            return null;
        }
        return partFileSizeUnitEnum.getDesc();
    }

    public static PartFileSizeUnitEnum forValue(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        for (PartFileSizeUnitEnum partFileSizeUnitEnum : PartFileSizeUnitEnum.values()) {
            if (partFileSizeUnitEnum.getValue().equals(value)) {
                return partFileSizeUnitEnum;
            }
        }
        return null;
    }

    public static PartFileSizeUnitEnum forDesc(String desc) {
        if (desc == null) {
            return null;
        }
        for (PartFileSizeUnitEnum partFileSizeUnitEnum : PartFileSizeUnitEnum.values()) {
            if (desc.equals(partFileSizeUnitEnum.getDesc())) {
                return partFileSizeUnitEnum;
            }
        }
        return null;
    }


    public static String getFileSizeWithUnit(Long fileSize) {
        if (fileSize == null || fileSize == 0L) {
            return "0" + KB.value;
        }

        double sizeInKB = fileSize.doubleValue() / 1024;
        double sizeInMB = sizeInKB / 1024;
        double sizeInGB = sizeInMB / 1024;

        String unit;
        double sizeWithPrecision;

        if (sizeInGB >= 1) {
            unit = GB.value;
            sizeWithPrecision = roundToFourDecimalPlaces(sizeInGB);
        } else if (sizeInMB >= 1) {
            unit = MB.value;
            sizeWithPrecision = roundToFourDecimalPlaces(sizeInMB);
        } else {
            unit = KB.value;
            sizeWithPrecision = roundToFourDecimalPlaces(sizeInKB);
        }
        return sizeWithPrecision + unit;
    }

    /**
     * 将double值四舍五入到小数点后四位
     */
    private static double roundToFourDecimalPlaces(double value) {
        DecimalFormat df = new DecimalFormat("#.####");
        return Double.parseDouble(df.format(value));
    }

    public static void main(String[] args) {
        System.out.println(getFileSizeWithUnit(null));
        System.out.println(getFileSizeWithUnit(0L));
        System.out.println(getFileSizeWithUnit(1L));
        System.out.println("-----");
        System.out.println(getFileSizeWithUnit(1023L));
        System.out.println(getFileSizeWithUnit(1024L));
        System.out.println(getFileSizeWithUnit(1025L));
        System.out.println("-----");
        System.out.println(getFileSizeWithUnit(1048575L));
        System.out.println(getFileSizeWithUnit(1048576L));
        System.out.println(getFileSizeWithUnit(1048577L));
        System.out.println(getFileSizeWithUnit(1049000L));
        System.out.println(getFileSizeWithUnit(1050000L));
        System.out.println("-----");
        System.out.println(getFileSizeWithUnit(1073741823L));
        System.out.println(getFileSizeWithUnit(1073741824L));
        System.out.println(getFileSizeWithUnit(1073741825L));
        System.out.println(getFileSizeWithUnit(1073800000L));
        System.out.println(getFileSizeWithUnit(1074000000L));
    }
}
